package com.wechat.scheme.entity.base;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;

import java.io.Serializable;

/**
 * 自定义UUID生成器
 * @author magina
 */
public class UUIDGenerator extends org.hibernate.id.UUIDGenerator {

    @Override
    public Serializable generate(SharedSessionContractImplementor session, Object object) throws HibernateException {
        if(object instanceof AbstractIdEntity){
            AbstractIdEntity o = (AbstractIdEntity) object;
            if (StringUtils.isNotBlank(o.getId())) {
                return (Serializable) o.getId();
            }
            Serializable uuid = super.generate(session, object);
            String id  =  uuid.toString().replace("-","");
            return id;
        }else{
            throw new RuntimeException("invalid extend type!");
        }
    }

}